<?php

namespace app\admin\controller\work;

use addons\wechat\library\Config as ConfigService;
use app\admin\model\Shop;
use app\admin\model\Shopuser;
use app\admin\model\User;
use app\admin\model\Useramount;
use app\admin\model\Usermsgweixin;
use app\common\controller\Backend;
use EasyWeChat\Factory;

/**
 * 用户发布工作
 *
 * @icon fa fa-circle-o
 */
class Publishwork extends Backend
{
    
    /**
     * Publishwork模型对象
     * @var \app\admin\model\Publishwork
     */
    protected $model = null;
    protected $wxapp = null;

    public function _initialize()
    {
        parent::_initialize();
        $this->model = new \app\admin\model\Publishwork;
        $this->wxapp = Factory::officialAccount(ConfigService::load());


    }

    public function import()
    {
        parent::import();
    }

    /**
     * 默认生成的控制器所继承的父类中有index/add/edit/del/multi五个基础方法、destroy/restore/recyclebin三个回收站方法
     * 因此在当前控制器中可不用编写增删改查的代码,除非需要自己控制这部分逻辑
     * 需要将application/admin/library/traits/Backend.php中对应的方法复制到当前控制器,然后进行修改
     */

    /**
     * 查看
     */
    public function index()
    {
        //设置过滤方法
        $this->request->filter(['strip_tags', 'trim']);
        if ($this->request->isAjax()) {
            //如果发送的来源是Selectpage，则转发到Selectpage
            if ($this->request->request('keyField')) {
                return $this->selectpage();
            }
            list($where, $sort, $order, $offset, $limit) = $this->buildparams();
            $list = $this->model->with(['user','worktype','address'])
                ->where($where)
                ->order($sort, $order)
                ->paginate($limit);
            foreach ($list as $k => $v) {

                if($v['status'] == -1){
                    $list[$k]['status'] = "未审核";
                }else if($v['status'] == 1){
                    $list[$k]['status'] = "审核通过";
                }else if($v['status'] == 0){
                    $list[$k]['status'] = "审核拒绝";
                }
            }
            $result = array("total" => $list->total(), "rows" => $list->items());

            return json($result);
        }
        return $this->view->fetch();
    }


    /**
     * 编辑
     */
    public function edit($ids = null)
    {
        $row = $this->model->get($ids);

        if ($this->request->isPost()) {

            $params = $this->request->post("row/a");

            if($params['status'] == 1){

                $usermsgModel = new Usermsgweixin();
                $wx = $usermsgModel->where('user_id',$row['uid'])->find();
                if($wx){
                    $t = $this->wxapp->template_message->send([
                        'touser' => $wx['openid'],
                        'template_id' => '4aQt-5F7lCj5HshlUPLz09Y1ARfJUefm4LMSA-HyeFg',
                        'data' => [
                            'first' => "你好，你的发布的工作有新的进度状态！",
                            'keyword1' => "职位审核",
                            'keyword2' => "已通过",
                            'remark' => '恭喜您！',
                        ],
                    ]);

                }

            }else{

            }
        }
        if (!$row) {
            $this->error(__('No Results were found'));
        }
        return parent::edit($ids);
    }


}
